***Dataset setup and prep***


**Create directories**

mkdir Graphs


**Create variables for analysis**

sort subject round

*Intensity and duration means

by subject: egen intensity_mean_all = mean(valuesp_blast_intensity)
by subject: egen duration_mean_all = mean(valuesp_blast_duration)
label variable intensity_mean_all "Subject's average intensity, all rounds"
label variable duration_mean_all "Subject's average duration, all rounds"

*Intensity and duration, standardized

gen intensityduration = (valuesp_blast_intensity + valuesp_blast_duration)/20
label variable intensityduration "Subject's combined intensity and duration, all rounds"
by subject: egen intensityduration_mean_all = mean(intensityduration)
label variable intensityduration_mean_all "Subject's average combined intensity and duration, all rounds"

*After wins and losses

sort subject round
gen wonlast = 0
replace wonlast = 1 if subject[_n-1] == subject & valuesp_win[_n-1] ==1
replace wonlast = . if round == 1
label variable wonlast "Subject won previous round"
label define wonlast 0 "Lost" 1 "Won"
label values wonlast wonlast

gen intensity_afterwins = valuesp_blast_intensity if wonlast == 1
gen intensity_afterloss = valuesp_blast_intensity if wonlast == 0
label variable intensity_afterwins "Intensity, after winning rounds only"
label variable intensity_afterloss "Intensity, after losing rounds only"
by subject: egen intensity_mean_afterwins = mean(intensity_afterwins)
by subject: egen intensity_mean_afterloss = mean(intensity_afterloss)
label variable intensity_mean_afterwins "Subject's average intensity, after winning rounds only"
label variable intensity_mean_afterloss "Subject's average intensity, after losing rounds only"

gen duration_afterwins = valuesp_blast_duration if wonlast == 1
gen duration_afterloss = valuesp_blast_duration if wonlast == 0
label variable duration_afterwins "Duration, after winning rounds only"
label variable duration_afterloss "Duration, after losing rounds only"
by subject: egen duration_mean_afterwins = mean(duration_afterwins)
by subject: egen duration_mean_afterloss = mean(duration_afterloss)
label variable duration_mean_afterwins "Subject's average duration, after winning rounds only"
label variable duration_mean_afterloss "Subject's average duration, after losing rounds only"

gen intensityduration_afterwins = intensityduration if wonlast == 1
gen intensityduration_afterloss = intensityduration if wonlast == 0
label variable intensityduration_afterwins "Combined intensity and duration, after winning rounds only"
label variable intensityduration_afterloss "Combined intensity and duration, after losing rounds only"
by subject: egen intensityduration_mean_afterwins = mean(intensityduration_afterwins)
by subject: egen intensityduration_mean_afterloss = mean(intensityduration_afterloss)
label variable intensityduration_mean_afterwins "Subject's combined intensity and duration, after winning rounds only"
label variable intensityduration_mean_afterloss "Subject's combined intensity and duration, after losing rounds only"

*High- and low-value rounds

gen hiintensity = 0
replace hiintensity = 1 if valuesp_blast_intensity == 8 | valuesp_blast_intensity == 9 | valuesp_blast_intensity == 10
by subject: egen highintensity_sum = sum(hiintensity)
label variable hiintensity "High-intensity setting (8, 9, or 10)"
label variable highintensity_sum "Subject's total high-intensity rounds (8, 9, or 10)"

gen hiduration = 0
replace hiduration = 1 if valuesp_blast_duration == 8 | valuesp_blast_duration == 9 | valuesp_blast_duration == 10
by subject: egen highduration_sum = sum(hiduration)
label variable hiduration "High-duration setting (8, 9, or 10)"
label variable highduration_sum "Subject's total high-duration rounds (8, 9, or 10)"

*Trait measures

gen trait_aggression = aggression_q1 + aggression_q2 + aggression_q3 + aggression_q4 + aggression_q5 + aggression_q6 + aggression_q7 + aggression_q8 + aggression_q9 + aggression_q10 + (aggression_q11*(-1)+6) + aggression_q12
replace trait_aggression = trait_aggression - 12
label variable trait_aggression "Trait aggressiveness"

gen trait_aggression_high = 0
sum trait_aggression, d
replace trait_aggression_high = 1 if trait_aggression > r(p50)
label variable trait_aggression_high "High trait aggressiveness"


**Variables for storing values for graphs**

gen x_r = .75
gen x_f = 1.25

gen t_meanr_int = .
gen t_upperr_int = .
gen t_lowerr_int = .
gen t_meanf_int = .
gen t_upperf_int = .
gen t_lowerf_int = .
gen pvalue_int = .

gen t_meanr_dur = .
gen t_upperr_dur = .
gen t_lowerr_dur = .
gen t_meanf_dur = .
gen t_upperf_dur = .
gen t_lowerf_dur = .
gen pvalue_dur = .

gen t_meanr_sta = .
gen t_upperr_sta = .
gen t_lowerr_sta = .
gen t_meanf_sta = .
gen t_upperf_sta = .
gen t_lowerf_sta = .
gen pvalue_sta = .

gen t_meanr_highint = .
gen t_upperr_highint = .
gen t_lowerr_highint = .
gen t_meanf_highint = .
gen t_upperf_highint = .
gen t_lowerf_highint = .
gen pvalue_highint = .

gen t_meanr_highdur = .
gen t_upperr_highdur = .
gen t_lowerr_highdur = .
gen t_meanf_highdur = .
gen t_upperf_highdur = .
gen t_lowerf_highdur = .
gen pvalue_highdur = .

gen t_meanr_stavm = .
gen t_upperr_stavm = .
gen t_lowerr_stavm = .
gen t_meanf_stavm = .
gen t_upperf_stavm = .
gen t_lowerf_stavm = .
gen pvalue_stavm = .


**Variable display format**

format trait_aggression intensity_mean_all duration_mean_all intensityduration_mean_all intensityduration intensity_mean_afterwins intensity_mean_afterloss duration_mean_afterwins duration_mean_afterloss intensityduration_mean_afterwins intensityduration_mean_afterloss  %4.2f

set cformat %4.2f
set sformat %4.2f

save, replace
